{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Thermal Speed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "\n",
    "import astropy.units as u\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "from plasmapy.formulary import (\n",
    "    Maxwellian_speed_1D,\n",
    "    Maxwellian_speed_2D,\n",
    "    Maxwellian_speed_3D,\n",
    ")\n",
    "from plasmapy.formulary.speeds import thermal_speed"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[thermal_speed]: ../../api/plasmapy.formulary.speeds.thermal_speed.rst#plasmapy.formulary.speeds.thermal_speed\n",
    "\n",
    "The [thermal_speed] function can be used to calculate the thermal velocity for a Maxwellian velocity distribution. There are three common definitions of the thermal velocity, which can be selected using the \"method\" keyword, which are defined for a 3D velocity distribution as\n",
    "\n",
    "- 'most_probable' <br>\n",
    "$v_{th} = \\sqrt{\\frac{2 k_B T}{m}}$\n",
    "\n",
    "- 'rms' <br>\n",
    "$v_{th} = \\sqrt{\\frac{3 k_B T}{m}}$\n",
    "\n",
    "- 'mean_magnitude' <br>\n",
    "$v_{th} = \\sqrt{\\frac{8 k_B T}{m\\pi}}$\n",
    "\n",
    "The differences between these velocities can be seen by plotitng them on a 3D Maxwellian speed distribution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "nbsphinx-thumbnail": {
     "tooltip": "Thermal Speeds"
    }
   },
   "outputs": [],
   "source": [
    "T = 1e5 * u.K\n",
    "speeds = np.linspace(0, 8e6, num=600) * u.m / u.s\n",
    "\n",
    "pdf_3D = Maxwellian_speed_3D(speeds, T=T, particle=\"e-\")\n",
    "\n",
    "fig, ax = plt.subplots(figsize=(4, 3))\n",
    "\n",
    "v_most_prob = thermal_speed(T=T, particle=\"e-\", method=\"most_probable\", ndim=3)\n",
    "v_rms = thermal_speed(T=T, particle=\"e-\", method=\"rms\", ndim=3)\n",
    "v_mean_magnitude = thermal_speed(T=T, particle=\"e-\", method=\"mean_magnitude\", ndim=3)\n",
    "\n",
    "ax.plot(speeds / v_rms, pdf_3D, color=\"black\", label=\"Maxwellian\")\n",
    "\n",
    "ax.axvline(x=v_most_prob / v_rms, color=\"blue\", label=\"Most Probable\")\n",
    "ax.axvline(x=v_rms / v_rms, color=\"green\", label=\"RMS\")\n",
    "ax.axvline(x=v_mean_magnitude / v_rms, color=\"red\", label=\"Mean Magnitude\")\n",
    "\n",
    "ax.set_xlim(-0.1, 3)\n",
    "ax.set_ylim(0, None)\n",
    "ax.set_title(\"3D\")\n",
    "ax.set_xlabel(\"|v|/|v$_{rms}|$\")\n",
    "ax.set_ylabel(\"f(|v|)\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Similar speeds are defined for 1D and 2D distributions. The differences between these definitions can be illustrated by plotting them on their respective Maxwellian speed distributions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pdf_1D = Maxwellian_speed_1D(speeds, T=T, particle=\"e-\")\n",
    "pdf_2D = Maxwellian_speed_2D(speeds, T=T, particle=\"e-\")\n",
    "\n",
    "dim = [1, 2, 3]\n",
    "pdfs = [pdf_1D, pdf_2D, pdf_3D]\n",
    "\n",
    "plt.tight_layout()\n",
    "fig, ax = plt.subplots(ncols=3, figsize=(10, 3))\n",
    "\n",
    "for n, pdf in enumerate(pdfs):\n",
    "    ndim = n + 1\n",
    "    v_most_prob = thermal_speed(T=T, particle=\"e-\", method=\"most_probable\", ndim=ndim)\n",
    "    v_rms = thermal_speed(T=T, particle=\"e-\", method=\"rms\", ndim=ndim)\n",
    "    v_mean_magnitude = thermal_speed(\n",
    "        T=T, particle=\"e-\", method=\"mean_magnitude\", ndim=ndim\n",
    "    )\n",
    "\n",
    "    ax[n].plot(speeds / v_rms, pdf, color=\"black\", label=\"Maxwellian\")\n",
    "\n",
    "    ax[n].axvline(x=v_most_prob / v_rms, color=\"blue\", label=\"Most Probable\")\n",
    "    ax[n].axvline(x=v_rms / v_rms, color=\"green\", label=\"RMS\")\n",
    "    ax[n].axvline(x=v_mean_magnitude / v_rms, color=\"red\", label=\"Mean Magnitude\")\n",
    "\n",
    "    ax[n].set_xlim(-0.1, 3)\n",
    "    ax[n].set_ylim(0, None)\n",
    "    ax[n].set_title(f\"{ndim:d}D\")\n",
    "    ax[n].set_xlabel(\"|v|/|v$_{rms}|$\")\n",
    "    ax[n].set_ylabel(\"f(|v|)\")\n",
    "\n",
    "\n",
    "ax[2].legend(bbox_to_anchor=(1.9, 0.8), loc=\"upper right\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
